package com.yb.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;

import javax.xml.transform.Result;

/**
 * 定义返回结果
 */
public class AppResult<T> {
    /**
     * @JsonInclude(JsonInclude.Include.ALWAYS) 是为了无论如何都要参与序列化.固定code,message,data的格式.
     */
    // 状态码
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private int code;

    // 描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;

    // 数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;


    /**
     * 返回成功
     * @return
     */
    public static AppResult success(){
        return new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }


    /**
     * 自定义message的成功
     * @param message
     * @return
     */
    public static AppResult success(String message){
        return new AppResult(ResultCode.SUCCESS.getCode(),message);
    }


    /**
     * 自定义数据的成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> AppResult<T> success(T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }

    /**
     * 自定义消息和数据的成功返回
     * @param message
     * @param data
     * @return
     * @param <T>
     */
    public static <T> AppResult<T> success(String message,T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message,data);
    }

    /**
     * 失败
     * @return
     */
    public static AppResult failed(){
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }

    public static AppResult failed(String message){
        return new AppResult(ResultCode.FAILED.getCode(), message);
    }

    public static AppResult failed(ResultCode resultCode){
      return new AppResult(resultCode.getCode(), resultCode.getMessage());
    }


    public void setCode(int code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    /**
     * 构造方法
     * @param code
     * @param message
     */
    public AppResult(int code, String message) {
        this(code,message,null);
    }

    /**
     * 构造方法
     * @param code
     * @param message
     * @param data
     */
    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}
